/*!
 @header CircleInPopoverViewController.m
 @author 周益
 @version 1.0.0 2013/01/31 Creation
 Copyright (c) 2013年 mypocketbaby. All rights reserved.
 */

#import "CircleInPopoverViewController.h"
#import "DBKit.h"
#import "JSONRequest.h"
#import "Config.h"
#import "Helper.h"
#import "JMFBase.h"

//  -业务逻辑细节说明-
//
//  @abstract       CircleInPopoverViewController功能较为简单，实现基本的uitableview方法即可。
//

//  @discussion

//  @notes


//  -类修改日志-
//  修改人   修改时间   针对版本   修改描述

@interface CircleInPopoverViewController ()
@end

@implementation CircleInPopoverViewController
@synthesize delegate = _delegate;
@synthesize circleId = _circleId;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //获得圈子信息
    [self requestCircleList];
    
    //去掉tableview的分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return _circleArray.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
        //圈子名称label
        UILabel *circleNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 2.0f, 110.0f, 35.0f)];
        circleNameLabel.backgroundColor = [UIColor clearColor];
        circleNameLabel.textColor = _SYS_COLOR_002;
        circleNameLabel.font = _SYS_FONT_005;
        circleNameLabel.textAlignment = UITextAlignmentCenter;
        [cell.contentView addSubview:circleNameLabel]; 
        
        //分割线
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.5f, 34.5f, 148.5f, 0.5f)];
        imageView.image = [UIImage imageNamed:@"com_bg_011"];
        [cell.contentView addSubview:imageView];
        
    }
    
    //填充
    int row = [indexPath row];
    
    UILabel *circleNameLabel = [cell.contentView subviews][0];
    
    //第一行为全部
    if (row == 0) {
        
        circleNameLabel.text = _L(@"title_10");   //所有
        
        if (_circleId == 0) {
            cell.contentView.backgroundColor = [[UIColor alloc] initWithRed:66.0f/255.0f green:69.0f/255.0f blue:66.0f/255.0f alpha:1];
        }else{
            cell.contentView.backgroundColor = [UIColor clearColor];
        }
        
    }else{
        
        NSDictionary *circle = _circleArray[row - 1];
        NSString *circleName = [circle valueForKey:@"name"];
        
        circleNameLabel.text = circleName;
        
        if (_circleId == [circle[@"id"] integerValue]) {

            cell.contentView.backgroundColor = [[UIColor alloc] initWithRed:66.0f/255.0f green:69.0f/255.0f blue:66.0f/255.0f alpha:1];

        }else{
            cell.contentView.backgroundColor = [UIColor clearColor];

        }
    }
    
    
    
    //自定义cell被选中的颜色
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.contentView.frame];
    cell.selectedBackgroundView.backgroundColor = [[UIColor alloc] initWithRed:66.0f/255.0f green:69.0f/255.0f blue:66.0f/255.0f alpha:1];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35.0f;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //获取点击的circle
    int row = [indexPath row];
    NSDictionary *circle;
    
    //响应用户点击，将用户点击的信息反馈给代理
    if (row == 0) {
        circle = nil;
    }else{
        circle = _circleArray[row - 1];
    }
    
    if ([_delegate respondsToSelector:@selector(reloadCircleDataIByID:)]) {
        [_delegate reloadCircleDataIByID:circle];
        
    }
}

#pragma mark - 私有方法

/*!
 @abstract 向服务器请求用户圈子列表的信息
 */
- (void)requestCircleList
{
    //请求所有的圈子信息
    JSONRequest *request = [[JSONRequest alloc] initWithURLString:_JSON_URL_CIRCLE_LIST];
    request.delegate = self;
    [request asynchronous];
}

#pragma mark - JSONRequest Delegate

- (void)requestDidLoadJSON:(NSDictionary *)dict
{
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:dict];
    
    if (jmf.status == JMStatusSucceed) {
        
        //获得items
        _circleArray = (NSArray *)jmf.data;
        
        
        //如果加上“所有圈子”一共有5个圈子以上就提供上下滑动，否则不支持滑动
        if (_circleArray.count <= 4) {
            self.tableView.scrollEnabled = NO;
            self.tableView.showsHorizontalScrollIndicator = NO;
        }else{
            self.tableView.scrollEnabled = YES;
            self.tableView.showsHorizontalScrollIndicator = YES;
        }
        
        //设置tableview数据源
        [self.tableView reloadData];
    }
    else {
        [Helper easyShowJMFStatus:jmf];
    }
}

- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:nil andMessage:_L(@"title_networkunavailabe")];
}
@end
